const Joi = require('joi');
const Action = require('../models/action.model');

const actionSchema = Joi.object({
  value: Joi.object().default({}),
})


module.exports = {
  insert,
  findOne, 
  findAll, 
  remove,
  edit,
}

async function insert(action) {
  console.log(action)
  action = await Joi.validate(action, actionSchema, { abortEarly: false });
  return await new Action(action).save();
}

async function findAll() {
  return Action.find({}, function(err, doc){
    if(err) {
      return err 
    }
    return doc
  }) 
}

async function findOne(id) {
  return Action.findOne({"_id": id}, function(err, doc){
    if(err) {
      return err 
    }
    return doc
  }) 
}

async function edit(action) {
  return Action.updateOne({"_id":action["_id"]}, {$set: action}, function(err, doc){
    if(err) {
      return false
    }
    return true
  });
}

async function remove(id) {
  return Action.deleteOne({"_id": id}, function(err, doc){
    if(err) {
      return false
    }
    return true
  }) 
}
